//
//  PassengerTableViewController.m
//  JiongTu
//
//  Created by 杨璟 on 16/4/7.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#import "PassengerTableViewController.h"

#import "JTBaseNaviController.h"
#import "PassengerDetailViewController.h"

#import "PassengerMsgCell.h"

#import "JTDriverTrip.h"
#import "JTPassengerTrip.h"
#import "JTConst.h"

#import <AVOSCloud.h>
#import <SVProgressHUD.h>

@interface PassengerTableViewController ()

@property (nonatomic, strong) NSMutableArray *tripArray;

@end

@implementation PassengerTableViewController

- (void)setDriverTrip:(JTDriverTrip *)driverTrip {
    _driverTrip = driverTrip;
    
    AVQuery *startQuery = [AVQuery queryWithClassName:kJTClassJourneyPassenger];
    [startQuery whereKey:@"Start" equalTo:driverTrip.start];
    
    AVQuery *endQuery = [AVQuery queryWithClassName:kJTClassJourneyPassenger];
    [endQuery whereKey:@"End" equalTo:driverTrip.end];
    
    AVQuery *timeQuery = [AVQuery queryWithClassName:kJTClassJourneyPassenger];
    [timeQuery whereKey:kJTJourneyKeyDate greaterThanOrEqualTo:driverTrip.time];
    
    AVQuery *orderQuery = [AVQuery queryWithClassName:kJTClassJourneyPassenger];
    [orderQuery whereKey:kJTJourneyKeyOrdered equalTo:@(0)];
    
    AVQuery *seatQuery = [AVQuery queryWithClassName:kJTClassJourneyPassenger];
    [seatQuery whereKey:kJTJourneyKeySeats lessThanOrEqualTo:driverTrip.seatCount];
    
    AVQuery *query = [AVQuery andQueryWithSubqueries:[NSArray arrayWithObjects:startQuery,endQuery,timeQuery,orderQuery,seatQuery,nil]];
    
    [SVProgressHUD showWithStatus:@"查询中"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
        if (!error && results.count) {
            self.tripArray = [JTPassengerTrip tripWithAVObjects:results];
            [SVProgressHUD showSuccessWithStatus:@"匹配成功"];
        } else if (!error && !results.count) {
            [SVProgressHUD showInfoWithStatus:@"暂无该方向的乘客，请到订单中查看状态"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        } else if (error) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self dismissViewControllerAnimated:YES completion:nil];
            });
        }
    }];
}

- (void)setTripArray:(NSMutableArray *)tripArray {
    _tripArray = tripArray;
    
    if (tripArray.count) {
        [self.tableView reloadData];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [SVProgressHUD dismiss];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tripArray.count;
}

- (PassengerMsgCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PassengerMsgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"passMsgCell" forIndexPath:indexPath];
    cell.oneTrip = self.tripArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 95;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    PassengerDetailViewController *vc = [sb instantiateViewControllerWithIdentifier:@"passDetailVC"];
//    
//    JTBaseNaviController *navi = [JTBaseNaviController navigationControllerWithRootViewController:vc];
//    [self presentViewController:navi animated:YES completion:^{
//        vc.oneTrip = self.tripArray[indexPath.row];
//    }];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"passDetail"]) {
        PassengerDetailViewController *vc = segue.destinationViewController;
        NSInteger selectedIndex = [[self.tableView indexPathForSelectedRow] row];
        vc.pushTrip = self.tripArray[selectedIndex];
    }
}

- (IBAction)dismissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:tmpDriverTripID];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }];
}

@end
